package com.example.knowman.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.knowman.ObService.*;
import com.example.knowman.ReturnMessage.*;
import com.example.knowman.dao.*;
import com.example.knowman.entity.*;
import com.example.knowman.service.*;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

//知识详情页面
@RestController
@RequestMapping("/knowDetails")
public class KnowDetailsController {
    private final OBKnowCategoryService obKnowCategoryService;
//
    private final OBknowArchFilesService oBknowArchFilesService;
//
    private final OBKnowMainService obKnowMainService;
//
    private final IKnowShareService iKnowShareService;

    private final OBknowLabelMainService oBknowLabelMainService;

    private final OBarchLabelMainService oBarchLabelMainService;

    private final OBlabelCategoryService oBlabelCategoryService;

    private final OBknowLabelDetailService oBknowLabelDetailService;
//
    public KnowDetailsController(IKnowShareService iKnowShareService,OBKnowMainService obKnowMainService,
            OBknowArchFilesService oBknowArchFilesService,OBKnowCategoryService obKnowCategoryService,
            OBknowLabelMainService oBknowLabelMainService,OBarchLabelMainService oBarchLabelMainService,
            OBlabelCategoryService oBlabelCategoryService,OBknowLabelDetailService oBknowLabelDetailService) {
        this.iKnowShareService = iKnowShareService;
        this.obKnowMainService = obKnowMainService;
        this.oBknowArchFilesService = oBknowArchFilesService;
        this.obKnowCategoryService = obKnowCategoryService;
        this.oBknowLabelMainService = oBknowLabelMainService;
        this.oBarchLabelMainService = oBarchLabelMainService;
        this.oBlabelCategoryService = oBlabelCategoryService;
        this.oBknowLabelDetailService = oBknowLabelDetailService;
    }


    //返回点赞数与我在这个知识中的点赞状态，输入knowId+myStaffCode
    @GetMapping("getPraise")
    public ResponseEntity<MessageEx_Praise_StateCount> getPraise(@RequestParam Integer knowId,
                                                                 @RequestParam String myStaffCode) {

        //得到knowMain中对应的点赞数
        Map<Integer,knowMain> knowMainAll = obKnowMainService.getMapAll();
        Integer praiseCount = knowMainAll.get(knowId).getPraiseCount();

        //得到knowShare的点赞状态
        knowShare item = iKnowShareService.getItemBy_KnowId_myStaffCode(knowId,myStaffCode);
        if(item == null){
            MessageEx_Praise_StateCount mes = new MessageEx_Praise_StateCount();
            mes.setCode(100);
            mes.setTip("knowShare对应数据还未插入！");
            return ResponseEntity.badRequest().body(mes);

        }
        Integer praise = item.getPraise();

        MessageEx_Praise_StateCount mes = new MessageEx_Praise_StateCount();
        PraiseCountAndPraiseState res = new PraiseCountAndPraiseState();
        res.setPraise(praise);
        res.setPraiseCount(praiseCount);
        mes.setCode(200);
        mes.setTip("查找成功");
        mes.setItem(res);


        return ResponseEntity.ok(mes);

    }


    //返回该知识下的所有评论+评论数（要不要返回是否已经评论）  //空指针异常有bug
    @GetMapping("getComments")
    public ResponseEntity<MessageKDC> getComments(@RequestParam Integer knowId,
                                                  @RequestParam String myStaffCode) {

        //得到所有knowId符合的knowShare
        List<knowShare> all = iKnowShareService.getByKnowId(knowId);
        List<TheComment> Comments = new ArrayList<>();
//        System.out.println(all);
        //取出符合knowId的数据，统计评论数量，赋值
        boolean flag = true;
        for(knowShare item :all){
//            System.out.println(item);
            if(item.getComment() != null && !item.getComment().isEmpty()){
                TheComment temp = new TheComment();
                temp.setStaffCode(item.getMyStaffCode());
                temp.setCreatetime(item.getCreatetime());
                temp.setContent(item.getComment());
//
                Comments.add(temp);
                if(temp.getStaffCode().equals(myStaffCode)){
                    flag= false;
                }
            }
        }
//        System.out.println(Comments);
        Integer num = Comments.size();
//
//

//
        knowDetailsComments res = new knowDetailsComments();
        res.setFlag(flag);
        res.setComments(Comments);
        res.setCommentNum(num);

        MessageKDC mes = new MessageKDC();
        mes.setCode(200);
        mes.setTip("查询成功");
        mes.setItem(res);

        return ResponseEntity.ok(mes);

//        return null;

    }

    //打标页面，输入knowId
    @GetMapping("MainDetails")
    public ResponseEntity<MessageExMainDetails> MainDetails(@RequestParam Integer knowId) {
        MessageExMainDetails mes = new MessageExMainDetails();
        MainDetails res = new MainDetails();

        //knowMain的knowId、staffCode（知识发布人）、knowTitle
        Map<Integer,knowMain> KMMap = obKnowMainService.getMapAll();
        knowMain itemKM = KMMap.get(knowId);
        res.setKnowId(itemKM.getKnowId());
        res.setKnowTitle(itemKM.getKnowTitle());
        res.setStaffCode(itemKM.getStaffCode());

        //附件表KnowArchFiles的List<主键、fileName、fileURL>
        List<knowArchFiles> lisFiles = oBknowArchFilesService.getByKnowId(knowId);
        List<KnowArchFilesSimple> lis = new ArrayList<>();
        for (knowArchFiles item : lisFiles){
            KnowArchFilesSimple temp = new KnowArchFilesSimple();
            temp.setArchFilesId(item.getArchFilesId());
            temp.setFileName(item.getFileName());
            temp.setFileURL(item.getFileURL());

            lis.add(temp);
        }
        res.setList(lis);





        //知识类别表KnowCategory的knowCateName（知识类别名）
        Map<Integer,String> KCGMap = obKnowCategoryService.getIdAndName();
        String knowCateName = KCGMap.get(itemKM.getKnowCateId());

        res.setKnowCateName(knowCateName);

        //打标Main（KnowLabelMain）的打标内容lblContent
        knowLabelMain KLM = oBknowLabelMainService.getSingle(knowId);
        String lblContent = KLM.getLblContent();
        res.setLblContent(lblContent);

        //打标详情以及标签List<标签名、标签颜色、打标内容>
        List<KnowLabelDetailSimple> DetailList = new ArrayList<>();
        //得到对应List<knowLabelDetail>
        List<knowLabelDetail> KLDs = oBknowLabelDetailService.Dabiaos(KLM.getKnowLblId());
        //根据这个list去找标签
        Map<Integer,labelCategory> LabelsMap=oBlabelCategoryService.getLabelDetails();
        for (knowLabelDetail item:KLDs){
            KnowLabelDetailSimple temp = new KnowLabelDetailSimple();
            temp.setDetaillblContent(item.getLblContent());
            temp.setLabelColor(LabelsMap.get(item.getLabelCateId()).getLabelColor());
            temp.setLabelName(LabelsMap.get(item.getLabelCateId()).getLabelName());

            DetailList.add(temp);
        }
        res.setDetailList(DetailList);
        //

        //mes赋值
        mes.setCode(200);
        mes.setTip("查询成功");
        mes.setItem(res);

        return ResponseEntity.ok(mes);
    }

    //archLabelMain详情页面，输入knowId
    @GetMapping("ArchMainDetails")
    public ResponseEntity<MessageExMainDetails> ArchMainDetails(@RequestParam Integer knowId) {
        MessageExMainDetails mes = new MessageExMainDetails();
        MainDetails res = new MainDetails();

        //knowMain的knowId、staffCode（知识发布人）、knowTitle
        Map<Integer,knowMain> KMMap = obKnowMainService.getMapAll();
        knowMain itemKM = KMMap.get(knowId);
        res.setKnowId(itemKM.getKnowId());
        res.setKnowTitle(itemKM.getKnowTitle());
        res.setStaffCode(itemKM.getStaffCode());

        //附件表KnowArchFiles的List<主键、fileName、fileURL>
        List<knowArchFiles> lisFiles = oBknowArchFilesService.getByKnowId(knowId);
        List<KnowArchFilesSimple> lis = new ArrayList<>();
        for (knowArchFiles item : lisFiles){
            KnowArchFilesSimple temp = new KnowArchFilesSimple();
            temp.setArchFilesId(item.getArchFilesId());
            temp.setFileName(item.getFileName());
            temp.setFileURL(item.getFileURL());

            lis.add(temp);
        }
        res.setList(lis);

        //知识类别表KnowCategory的knowCateName（知识类别名）
        Map<Integer,String> KCGMap = obKnowCategoryService.getIdAndName();
        String knowCateName = KCGMap.get(itemKM.getKnowCateId());

        res.setKnowCateName(knowCateName);

        //打标Main（archLabelMain）的打标内容lblContent
//        knowLabelMain KLM = oBknowLabelMainService.getSingle(knowId);
        archLabelMain ALM = oBarchLabelMainService.getSingle(knowId);
        String lblContent = ALM.getLblContent();
        res.setLblContent(lblContent);

        //mes赋值
        mes.setCode(200);
        mes.setTip("查询成功");
        mes.setItem(res);

        return ResponseEntity.ok(mes);
    }

    //点赞-抄一下knowShare的
    @GetMapping("praise")
    public ResponseEntity<Message> praise(@RequestParam Integer knowId,
                                          @RequestParam String myStaffCode) {
        Message mes = new Message();
        //修改knowShare的点赞状态
        iKnowShareService.praiseByKS(knowId,myStaffCode);

        //knowMain对应点赞数+1 (根据knowId)
        obKnowMainService.addPraise(knowId);


        mes.setCode(200);
        mes.setTip("点赞成功");

        return ResponseEntity.ok(mes);
    }

    //评论，knowMain评价数+1
    @GetMapping("updateComment")
    public ResponseEntity<Message> updateComment(@RequestParam Integer knowId,
                                                @RequestParam String myStaffCode,
                                                 @RequestParam String comment) {
        Message mes = new Message();
        if(comment != null && !comment.isEmpty()){
            //修改knowShare的点赞状态
            iKnowShareService.updateCommentByKS(knowId,myStaffCode,comment);

            //knowMain对应点赞数+1 (根据knowId)
            obKnowMainService.addCommentCount(knowId);

            mes.setCode(200);
            mes.setTip("评论成功");
            return ResponseEntity.ok(mes);
        }
        else {
            mes.setCode(100);
            mes.setTip("评价不能为空");
            return ResponseEntity.badRequest().body(mes);
        }


    }

    //阅读,knowMain阅读数+1
    @GetMapping("updateReadState")
    public ResponseEntity<Message> updateComment(@RequestParam Integer knowId,
                                                 @RequestParam String myStaffCode) {
        Message mes = new Message();

        //修改knowShare的点赞状态
        iKnowShareService.updateReadStateByKS(knowId,myStaffCode);

        //knowMain对应点赞数+1 (根据knowId)
        obKnowMainService.addReadCount(knowId);

        mes.setCode(200);
        mes.setTip("阅读成功");
        return ResponseEntity.ok(mes);



    }

}
